// run: $exec < input
// opt: 0
// flag: -g
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>

int const maxn = 30;
int head[maxn], end[maxn * maxn], next[maxn * maxn];
int in[maxn], tin[maxn];
int alloc = 2;

int n, m;

void add_edge(int u, int v)
{
	end[alloc] = v; next[alloc] = head[u]; head[u] = alloc++;
	in[v]++;
}

bool check(int u, int v)
{
	for (int p = head[u]; p; p = next[p]) {
		int w = end[p];
		if (w == v) return true;
		if (check(w, v)) return true;
	}
	return false;
}

int main()
{
	while (std::cin >> n >> m && (n || m)) {
		alloc = 2;
		for (int i = 0; i < n; i++) head[i] = in[i] = 0;

		bool ok = true, seq = false;
		for (int i = 1; i <= m; i++) {
			char x, y;
			std::cin >> x >> y >> y;
			if (seq || !ok) continue;
			x -= 'A'; y -= 'A';
			if (ok && check(x, y)) {
				std::cout << "Inconsistency found after " << i << " relations.\n";
				ok = false;
				continue;
			}
			add_edge(y, x);

			for (int i = 0; i < n; i++) tin[i] = in[i];
			seq = true;
			std::queue<int> q;
			for (int i = 0; i < n; i++)
				if (!tin[i]) q.push(i);
			std::string ans;
			while (!q.empty()) {
				if (q.size() > 1) { seq = false; break; }
				int now = q.front(); q.pop();
				ans += char('A' + now);
				for (int p = head[now]; p; p = next[p]) {
					int v = end[p];
					tin[v]--;
					if (!tin[v]) q.push(v);
				}
			}
			if (!seq) continue;
			std::reverse(ans.begin(), ans.end());
			std::cout << "Sorted sequence determined after " << i << " relations: " << ans << ".\n";
		}
		if (!seq && ok) std::cout << "Sorted sequence cannot be determined.\n";
	}
}

